import { ref } from 'vue';

export interface PublicQueryState {
  callTimeStart?: string; // 开始时间
  callTimeEnd?: string; // 结束时间
  callSummaryCodes?: string; //接警类别(筛选条件)
  dealSummaryCodes?: string; //反馈类别(筛选条件)
  xqdwCodes?: string; //辖区单位(筛选条件)
  classifyCodes?: string; // 智能分类(筛选条件)

  callSummaryCode?: string; //接警类别(图表)
  dealSummaryCode?: string; //反馈类别(图表)
  xqdwCode?: string; //辖区单位(图表)

  searchType?: string; // 区分传统和智能
  inputParams?: string; // 搜索内容
  inputType?: string; // 搜索框类型
  page?: number;
  size?: number;
  dealFlag?: string; // AI研判 点击图表查看警情列表,警情列表是否显示反馈内容
  [proppName: string]: any;
  //常规统计分析
  otherFlag?: string; //其他
  trafficFlag?: string; //交通
}

export const base64DataURLToArrayBuffer = dataURL => {
  const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
  if (!base64Regex.test(dataURL)) {
    return false;
  }
  const stringBase64 = dataURL.replace(base64Regex, '');
  let binaryString;
  if (typeof window !== 'undefined') {
    binaryString = window.atob(stringBase64);
  }
  // node环境中使用下面代码
  //   else {
  //     binaryString = new Buffer(stringBase64, 'base64').toString('binary');
  //   }
  const len = binaryString.length;
  const bytes = new Uint8Array(len);
  for (let i = 0; i < len; i++) {
    const ascii = binaryString.charCodeAt(i);
    bytes[i] = ascii;
  }
  return bytes.buffer;
};

const ageName = value => {
  if (value == '未成年') {
    return value + '（1-17岁）';
  } else if (value == '青年') {
    return value + '（18-45岁）';
  } else if (value == '中年') {
    return value + '（46-69岁）';
  } else if (value == '老年') {
    return value + '（69岁以上）';
  } else if (value == '男') {
    return value + '性';
  } else if (value == '女') {
    return value + '性';
  }
};

export const personnelAge = (value, index) => {
  const ageList = ref<any>([]);
  value.forEach(item => {
    if (item.name != '未填报') {
      ageList.value.push({
        name: ageName(item.name),
        value: '涉警人数为' + item.value + '人',
        percent: item.percent,
      });
    } else {
      ageList.value.push({
        name: item.name,
        value:
          (index === 1 ? '人员年龄段及相关信息的的警情数为' : '人员性别及相关信息的的警情数为') +
          item.value +
          '起',
        percent: item.percent,
      });
    }
  });
  return plusSymbol(ageList.value);
};

//添加符号；。
export const plusSymbol = value => {
  const valueList = value;
  for (let index = 0; index < valueList.length; index++) {
    valueList[index].symbol = '；';
  }
  valueList[valueList.length - 1].symbol = '。';
  return valueList;
};

//判断是否展示TOP
export const top = (index, length) => {
  if (length == index) {
    return 'TOP' + index;
  } else {
    return '';
  }
};

export const base64 = base64 => {
  return new Promise(resolve => {
    const images = new Image();
    images.src = base64;
    images.onload = () => {
      const obj = {
        width: images.width,
        height: images.height,
      };
      resolve(obj);
    };
  });
};

export const increase = num => {
  if (num < 0) {
    return ' ' + num;
  } else {
    return num;
  }
};

//排序
export const sort = (array, key) => {
  return array.sort((a, b) => {
    const x = b[key];
    const y = a[key];
    return x < y ? -1 : x > y ? 1 : 0;
  });
};

//同环比判断
export const thJudge = (chainPercent, yoyPercent) => {
  const value =
    '环比' +
    (parseFloat(chainPercent) > 0
      ? '上升' + chainPercent + '%'
      : chainPercent == '-'
      ? chainPercent + '%'
      : '下降' + chainPercent.split('-')[1] + '%') +
    '，同比' +
    (parseFloat(yoyPercent) > 0
      ? '上升' + yoyPercent + '%'
      : yoyPercent == '-'
      ? yoyPercent + '%'
      : '下降' + yoyPercent.split('-')[1] + '%');
  return value;
};

//时间段判断
export const groupFieldTime = value => {
  if (value == 'hour') {
    return '时';
  } else if (value == 'day') {
    return '日';
  } else if (value == 'month') {
    return '月';
  } else if (value == 'year') {
    return '年';
  }
};
